package com.come4loves.game.module.skill.helper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.come4loves.game.module.card.constant.CardType;
import com.come4loves.game.module.fight.action.BatchAction;
import com.come4loves.game.module.fight.constants.SkillMoment;
import com.come4loves.game.module.fight.helper.ActionRecorder;
import com.come4loves.game.module.fight.helper.FightHelper;
import com.come4loves.game.module.fight.model.ActorBattle;
import com.come4loves.game.module.fight.model.FightCard;
import com.come4loves.game.module.fight.model.FightContext;
import com.come4loves.game.module.fight.model.MainCardArea;
import com.come4loves.game.module.skill.AbstractSkillActorParser;
import com.come4loves.game.module.skill.AbstractSkillCardParser;
import com.come4loves.game.module.skill.AbstractSkillTargetActor;
import com.come4loves.game.module.skill.model.SkillModel;

public class SkillHelper {
	public static Logger LOGGER = LoggerFactory.getLogger(SkillHelper.class);

	public static void handlerSkillModel(MainCardArea mainCardArea, SkillMoment moment, ActionRecorder actionRecorder, ActorBattle attacker, Collection<ActorBattle> allActorBattles,
			List<FightCard> chooseTargetsCard) {
		if (mainCardArea.getWeaponCard()!=null){
			handlerSkillModel(mainCardArea.getWeaponCard(), moment, actionRecorder, attacker, allActorBattles, chooseTargetsCard);
		}
		if (mainCardArea.getDefCard()!=null){
			handlerSkillModel(mainCardArea.getDefCard(), moment, actionRecorder, attacker, allActorBattles, chooseTargetsCard);
		}
		if (mainCardArea.getItemCard()!=null){
			handlerSkillModel(mainCardArea.getItemCard(), moment, actionRecorder, attacker, allActorBattles, chooseTargetsCard);
		}
		
	}
	/**
	 * 处理技能
	 * 
	 * @param fightCard
	 * @param actionRecorder
	 * @param battleContext
	 */
	public static void handlerSkillModel(FightCard fightCard, SkillMoment moment, ActionRecorder actionRecorder, ActorBattle attacker, Collection<ActorBattle> allActorBattles,
			List<FightCard> chooseTargetsCard) {
		List<SkillModel> models = fightCard.getSkill(moment);
		if (models != null && !models.isEmpty()) {
			for (SkillModel model : models) {
				if (!model.getTargets().isEmpty()) {
					if (model.getTargets().containsKey("targetplayer")) {
						AbstractSkillTargetActor tHandler = AbstractSkillTargetActor.get(model.getTargets().get("targetplayer"));
						List<ActorBattle> targets = tHandler.getTargets(attacker, allActorBattles);
						for (ActorBattle actorBattle : targets) {
							// 对角色处理
							for (Entry<String, String> m : model.getEffect().entrySet()) {
								AbstractSkillActorParser handler = AbstractSkillActorParser.get(m.getKey());
								if (handler == null) {
									LOGGER.error("skill actor handler not implemets,id:{}", m.getKey());
									continue;
								}
								FightContext fightContext = new FightContext(actionRecorder, attacker,actorBattle, allActorBattles, chooseTargetsCard);
								handler.handerSkill(model, fightContext);
							}

						}
					} else {
						int target = model.getTargets().get("target");
						Collection<FightCard> result = null;
						if (target == -1) {
							result = new ArrayList<FightCard>();
							if (chooseTargetsCard != null && !chooseTargetsCard.isEmpty()) {
								result.addAll(chooseTargetsCard);
							}
						} else if (target == 0) {
							result = new ArrayList<FightCard>();
							result.add(fightCard);
						} else {
							result = TargetHelper.getTargets(fightCard, model.getTargets(), attacker, allActorBattles);
						}

						for (Entry<String, String> m : model.getEffect().entrySet()) {
							AbstractSkillCardParser parser = AbstractSkillCardParser.get(m.getKey());
							if (parser == null) {
								LOGGER.error("skill card parser not implemets,id:{}", m.getKey());
								continue;
							}
//							SkillAction skillAction = new SkillAction(fightCard, result);
//							actionRecorder.addAction(skillAction);
							BatchAction batchAction = new BatchAction();
							for (FightCard card : result) {
								FightContext fightContext = new FightContext(actionRecorder, attacker, getActorBattle(card, allActorBattles), allActorBattles, chooseTargetsCard);
								parser.handerSkill(fightCard, card, model, batchAction, fightContext);
							}
							actionRecorder.addAction(batchAction);

							for (FightCard card : result) {
								if (card.isDead()) {
									if (card.getCardType().equals(CardType.EQUIP)){
										MainCardArea mca = getMainCardArea(card, allActorBattles);
										mca.takeOff(card, actionRecorder);
									} else {
										FightHelper.createDeadAction(card, actionRecorder);
										if (card.getId() == fightCard.getId()) {
											continue;
										}
										SkillHelper.handlerSkillModel(card, SkillMoment.UNITLEFTED, actionRecorder, attacker, allActorBattles, chooseTargetsCard);
									}
								}
							}
						}
					}

				}
			}
		}
	}
	
	public static ActorBattle getActorBattle(FightCard fightCard, Collection<ActorBattle> allActorBattles) {
		for (ActorBattle actorBattle : allActorBattles) {
			for (FightCard card : actorBattle.getAllCards()) {
				if (card.equals(fightCard)){
					return actorBattle;
				}
			}
		}
		return null;
		
	}
	public static MainCardArea getMainCardArea(FightCard fightCard, Collection<ActorBattle> allActorBattles) {
		for (ActorBattle actorBattle : allActorBattles) {
			for (MainCardArea mainCardArea : actorBattle.getMainCardAreas()) {
				if (fightCard.equals(mainCardArea.getWeaponCard())|| fightCard.equals(mainCardArea.getDefCard())|| fightCard.equals(mainCardArea.getItemCard())){
					return mainCardArea;
				}
			}
		}
		return null;
		
	}

}
